const config = require("config.js")
const server_api = require("server_api.js")
const api_list = server_api.api_list
//统一的网络请求方法
function request(params, isGetTonken) {
  // 全局变量
  var globalData = getApp().globalData
  // 如果正在进行登陆，就将非登陆请求放在队列中等待登陆完毕后进行调用
  var accountInfo = wx.getAccountInfoSync();
  if (!isGetTonken && globalData.isLanding) {
    globalData.requestQueue.push(params)
    return
  }
  wx.request({
    url: config.domain + params.url, //接口请求地址
    data: params.data,
    header: {
      // application/json;charset=utf-8
      'Content-Type': params.content_type == undefined ? 'application/jsoncharset=utf-8' : params.content_type,
      'Access-Token': params.login ? undefined : wx.getStorageSync('token')
    },
    method: params.method == undefined ? "POST" : params.method,
    dataType: 'json',
    responseType: params.responseType == undefined ? 'text' : params.responseType,
    success: function (res) {
      wx.hideLoading()
      if (res.data.error_code == 0) {
        //如果有定义了params.callBack，则调用 params.callBack(res.data)
        if (params.callBack) {
          params.callBack(res.data)
        }
      } else if (res.data.error_code == 500) {
        wx.showToast({
          title: "服务器出了点小差",
          icon: "none",
        })
      } else if (res.data.error_code == 500) {
        wx.showToast({
          title: "服务器出了点小差",
          icon: "none",
        })
      } else if (res.data.error_code == 401) {
        wx.navigateTo({
          url: '/pages/login/login',
        })
      } else if (res.data.code == 400) {
        wx.showToast({
          title: res.msg,
          icon: "none"
        })
      } else {
        if(res.data.msg=='ble not enabled'){
          wx.showModal({
            title: '接口提示',
            content: '请打开蓝牙！！！',
            showCancel: false,
            complete: (res) => {
              return
            }
          })
        }else{
          wx.showModal({
            title: '接口提示',
            content: res.data.msg,
            showCancel: false,
            complete: (res) => {
              return
            }
          })
        }
       
      }
      if (!globalData.isLanding) {}
    },
    fail: function (err) {
    //   console.log(err)
    //   console.log('hideLoading23')
      wx.hideLoading()
      wx.showToast({
        title: "服务器出了点小差",
        icon: "none",
      })
    }
  })
}

//通过code获取token,并保存到缓存
var getToken = function () {
    wx.login({
      success: res => {
        request({
        login: true,
        url: api_list.login,
        data: {
          code: res.code,
          token: ''
        },
        callBack: result => {
          console.log('login_result',result)
          wx.setStorageSync('token', result.data.access_token) //把token存入缓存，请求接口数据时要用
          if (result.error_code == 3) {
            wx.showModal({
              showCancel: false,
              title: '提示',
              content: '您已被禁用，请联系客服'
            })
          }
          var app = getApp()
          var globalData = app.globalData
          globalData.isLanding = false
          globalData.user_info.nickname = result.nickname
          //完成登录，执行队列中的请求
          while (globalData.requestQueue.length) {
            request(globalData.requestQueue.pop())
          }
        }
      }, true)

    }
  })
}

//获取购物车商品数量
function getCartCount() {
  var params = {
    url: "/p/shopCart/prodCount",
    method: "GET",
    data: {},
    callBack: function (res) {
      if (res > 0) {
        wx.setTabBarBadge({
          index: 2,
          text: res + "",
        })
        var app = getApp()
        app.globalData.totalCartCount = res
      } else {
        wx.removeTabBarBadge({
          index: 2
        })
        var app = getApp()
        app.globalData.totalCartCount = 0
      }
    }
  }
  request(params)
}

exports.getToken = getToken
exports.request = request
exports.getCartCount = getCartCount
